//
//  SingerViewController.m
//  GSG
//
//  Created by dllo on 15/4/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "SingerViewController.h"
#import "AFNetworking.h"
#import "MusicModel.h"
#import "UIImageView+WebCache.h"
#import "SingCollectionViewCell.h"
#import "SinClassViewController.h"
#import"PlayListViewController.h"
#import "MBProgressHUD.h"
@interface SingerViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic,retain)NSMutableArray *modelArr;
@property(nonatomic,retain)MBProgressHUD*HUD;
@property (nonatomic,retain)UICollectionView *myCollectionView;
@end

@implementation SingerViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"歌手";
        self.modelArr = [NSMutableArray array];
    }

    return self;
}

- (void)data{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://v1.ard.tj.itlily.com/ttpod?a=getnewttpod&id=46&app=ttpod&v=v7.7.0.2015012818&uid=&mid=iPhone7%2C2&f=f320&s=s310&imsi=&hid=&splus=8.1.2&active=1&net=2&openudid=de3da938b2edbfc60f8a99d225d3fa93b9f9aadc&idfa=5B573856-7D24-475E-9673-33B14BF5623E&utdid=VP+JXmLCbp4DAPajlznU24QL&alf=201200" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSDictionary *dict = responseObject;
        
        NSMutableArray *arr = [dict objectForKey:@"data"];
        for (NSInteger i=0; i<arr.count; i++) {
            
            MusicModel *movie = [[MusicModel alloc] init];
            NSMutableDictionary *movieDic = arr[i];
            [movie setValuesForKeysWithDictionary:movieDic];
            [self.modelArr addObject:movie];

            
        }
        [self.myCollectionView reloadData];
        
        [_HUD hide:YES];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
     
        
    }];
    
}
-(void)actionBuLand{
    PlayListViewController *plvc=[[PlayListViewController alloc]init];
    [self.navigationController pushViewController:plvc animated:YES];
    [plvc release];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"播放" style:UIBarButtonItemStyleDone target:self action:@selector(actionBuLand)];

    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bujing.png"]];
    
        [self data];
    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc] init];
    
    flowlayout.itemSize = CGSizeMake(self.view.frame.size.width * 100 / 375, self.view.frame.size.height * 110 / 606);
    
    flowlayout.minimumLineSpacing = 40;
    
    flowlayout.minimumInteritemSpacing = 10;
    
    flowlayout.footerReferenceSize = CGSizeMake(self.view.frame.size.width * 130 / 375, self.view.frame.size.height * 100 / 667);
    
    flowlayout.sectionInset = UIEdgeInsetsMake(self.view.frame.size.width * 20 / 375, self.view.frame.size.height * 20 / 667, self.view.frame.size.width * 20 / 375, self.view.frame.size.height * 20 / 667);
    
    self.myCollectionView = [[UICollectionView alloc] initWithFrame:[[UIScreen mainScreen] bounds] collectionViewLayout:flowlayout];
    [self.view addSubview:self.myCollectionView];
    self.myCollectionView.backgroundColor = [UIColor clearColor];
    
    
    self.myCollectionView.dataSource = self;
    self.myCollectionView.delegate = self;
    
    
    [self.myCollectionView registerClass:[SingCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];

    //小菊花
    //方式1.直接在View上show
    _HUD = [[MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];
    
    
    //常用的设置
    //小矩形的背景色
    _HUD.color = [UIColor clearColor];//这儿表示无背景
    //显示的文字
    _HUD.labelText = @"提示";
    //细节文字
    _HUD.detailsLabelText = @"正在加载内容";
    //是否有庶罩
    _HUD.dimBackground = YES;

}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.modelArr.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    SingCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    MusicModel *music = self.modelArr[indexPath.row];
    cell.myLabel.text = music.title;
    NSURL *url = [NSURL URLWithString:music.pic_url];
    [cell.myImageView sd_setImageWithURL:url];
    return cell;

}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    SinClassViewController *sinClass = [[SinClassViewController alloc] init];
    sinClass.musicModel = self.modelArr[indexPath.row] ;

    [self.navigationController pushViewController:sinClass animated:YES];
    [sinClass release];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
